package LeetCode;

import java.util.BitSet;
import java.util.HashSet;

public class LC_409_LongestPalindrome {

    public static void main(String[] args) {

    }

    static class Solution {
        private int getIndex(char ch) {
            return (int) ch >= 'a' ? ch - 'a' + ('Z' - 'A') : ch - 'A';
        }

        public int longestPalindrome(String s) {
            int ret = 0;
            BitSet bs = new BitSet(26 * 2);
            for (int i = 0; i < s.length(); i++) {
                int index = getIndex(s.charAt(i));
                bs.set(index, !bs.get(index));
                if (!bs.get(index)) ret += 2;
            }
            if (!bs.isEmpty()) ret += 1;
            return ret;
        }

    }


    static class Solution_2 {
        public int longestPalindrome(String s) {
            if (s == null || s.length() == 0) return 0;

            int count = 0;
            HashSet<Character> set = new HashSet<>();
            for (int i = 0; i < s.length(); i++) {
                if (set.contains(s.charAt(i))) {
                    set.remove(s.charAt(i));
                    count++;
                } else {
                    set.add(s.charAt(i));
                }
            }

            if (!set.isEmpty()) return count * 2 + 1;
            return count * 2;
        }

    }
}